import 'package:get/get.dart';

import '../../../../../../common/api/x_processplatform_assemble_surface.dart';
import '../../../../../../common/models/enums/o2_native_app.dart';
import '../../../../../../common/models/process/office_center_filter.dart';
import '../../../../../../common/utils/log_util.dart';
import 'index.dart';

class OfficeCenterFilterAttributeController extends GetxController {
  OfficeCenterFilterAttributeController();

  final state = OfficeCenterFilterAttributeState();
  String? _type;

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    final map = Get.arguments;
    if (map != null ) {
      if (map['result'] != null) {
         state.filterPicked.value = OfficeCenterFilterBody.fromJson(map['result']);
      }
      if (map['type'] != null) {
        _type = map['type'] as String;
      }
      loadAttribute();
    }
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  Future<void> loadAttribute() async {
    OfficeCenterFilterAttribute? result;
    if (O2NativeAppEnum.task.key == _type) {
      result = await ProcessSurfaceService.to.getMyTaskFilterAttribute();
    } else if (O2NativeAppEnum.taskcompleted.key == _type) {
      result =
          await ProcessSurfaceService.to.getMyTaskCompletedFilterAttribute();
    } else if (O2NativeAppEnum.read.key == _type) {
      result = await ProcessSurfaceService.to.getMyReadFilterAttribute();
    } else if (O2NativeAppEnum.readcompleted.key == _type) {
      result =
          await ProcessSurfaceService.to.getMyReadCompletedFilterAttribute();
    }
    _setAttribute(result);
  }

  void _setAttribute(OfficeCenterFilterAttribute? attr) {
    if (attr == null) {
      return;
    }
    state.applicationList.clear();
    state.applicationList.addAll(attr.applicationList ?? []);
    state.processList.clear();
    state.processList.addAll(attr.processList ?? []);
    state.creatorUnitList.clear();
    state.creatorUnitList.addAll(attr.creatorUnitList ?? []);
    state.startTimeMonthList.clear();
    state.startTimeMonthList.addAll(attr.startTimeMonthList ?? []);
    state.completedTimeMonthList.clear();
    state.completedTimeMonthList.addAll(attr.completedTimeMonthList ?? []);
    state.activityNameList.clear();
    state.activityNameList.addAll(attr.activityNameList ?? []);
    state.completedList.clear();
    state.completedList.addAll(attr.completedList ?? []);
  }

  void clickItem(OfficeCenterFilterAttributeType type,
      OfficeCenterFilterAttributeItem item) {
    final picked = state.filterPicked.value;
    switch (type) {
      case OfficeCenterFilterAttributeType.applicationList:
        picked.applicationList =
            _checkItemValue(picked.applicationList, item);
        break;
      case OfficeCenterFilterAttributeType.processList:
        picked.processList =
            _checkItemValue(picked.processList, item);
        break;
      case OfficeCenterFilterAttributeType.activityNameList:
        picked.activityNameList =
            _checkItemValue(picked.activityNameList, item);
        break;
      case OfficeCenterFilterAttributeType.creatorUnitList:
        picked.creatorUnitList =
            _checkItemValue(picked.creatorUnitList, item);
        break;
      case OfficeCenterFilterAttributeType.startTimeMonthList:
        picked.startTimeMonthList =
            _checkItemValue(picked.startTimeMonthList, item);
        break;
      case OfficeCenterFilterAttributeType.completedTimeMonthList:
        picked.completedTimeMonthList = _checkItemValue(
            picked.completedTimeMonthList, item);
        break;
      case OfficeCenterFilterAttributeType.completedList:
        final list = picked.completedList ?? [];
        final value = item.value == null ? null : (item.value as bool);
        if (value != null) {
          if (list.contains(value)) {
            list.remove(value);
          } else {
            list.add(value);
          }
          picked.completedList = list;
        }
        break;
    }
    state.filterPicked.value = picked;
    update();
  }

  List<String> _checkItemValue(
      List<String>? bzList, OfficeCenterFilterAttributeItem item) {
    final list = bzList ?? [];
    final value = item.value == null ? '' : (item.value as String);
    if (list.contains(value)) {
      list.remove(value); // 如果列表中存在该字符串，删除它
    } else {
      list.add(value); // 如果列表中不存在该字符串，添加它
    }
    return list;
  }

  void clickCancel() {
    state.filterPicked.value = OfficeCenterFilterBody();
    update();
  }

  void clickPositive() {
    Get.back(result: state.filterPicked.value);
  }
}
